<template>
    <div class="content">
        <div class="chart-div">
            <van-row>
                <van-col :class="{'active':isActive1}" span="5" @click="one"><div class="header">病例来源构成</div></van-col>
                <van-col :class="{'active':isActive2}" span="5" @click="two"><div class="header">病例发现途径</div></van-col>
                <van-col :class="{'active':isActive3}" span="5" @click="three"><div class="header">二代病例构成</div></van-col>
                <van-col :class="{'active':isActive4}" span="5" @click="four"><div class="header">年龄分布</div></van-col>
                <van-col :class="{'active':isActive5}" span="5" @click="five"><div class="header">发病数</div></van-col>
            </van-row>
        </div>
        <div v-if="isActive1" class="bg-white">
            <div class="chart-div1">
                <div id="wuhan" class="chart-item-1"></div>
            </div>
            <div class="chart-div2">
                <van-row>
                    <van-col span="8"><div id="circular1" class="chart-item-2"></div></van-col>
                    <van-col span="8"><div id="circular2" class="chart-item-2"></div></van-col>
                    <van-col span="8"><div id="circular3" class="chart-item-2"></div></van-col>
                </van-row>
<!--                <div class="divider"></div>-->
            </div>
        </div>
        <div v-if="isActive2" class="bg-white">
            <div class="chart-div">
                <div id="case" class="chart-item-1"></div>
<!--                <div class="divider"></div>-->
            </div>
        </div>
        <div v-if="isActive3" class="bg-white">
            <div class="chart-div">
                <div id="generation" class="chart-item-1"></div>
<!--                <div class="divider"></div>-->
            </div>
        </div>
        <div v-if="isActive4" class="bg-white">
            <div class="chart-div">
                <div id="age" class="chart-item-1"></div>
<!--                <div class="divider"></div>-->
            </div>
        </div>
        <div v-if="isActive5" class="bg-white">
            <div class="chart-div">
                <div id="morbidity" class="chart-item-1"></div>
<!--                <div class="divider"></div>-->
            </div>
        </div>
    </div>
</template>

<script>
import { Dialog } from 'vant'
import { srdlyList, grtjList, blfxtjList, edblgcList, nlfbList, nlxbList } from '@/api/epidemic-surveillance/case-analysis'
import echarts from 'echarts'
export default {
  name: 'index',
  data () {
    return {
      list: [],
      active: 0,
      isActive1: true,
      isActive2: false,
      isActive3: false,
      isActive4: false,
      isActive5: false
    }
  },
  methods: {
    async one () {
      let result = await this.getOneData()
      console.log(result)
      setTimeout(() => {
        this.circular2(result.data[0])
        this.circular3(result.data[1])
        this.wuhan(result)
        this.circular1(result.data[2])
      }, 0)
      this.isActive1 = true
      this.isActive2 = false
      this.isActive3 = false
      this.isActive4 = false
      this.isActive5 = false
    },
    async getOneData () {
      let res = await Promise.all([srdlyList(), grtjList()])
      const { data } = res[0]
      if (data.errcode === 0) {
        const list = data.data
        let params = { wuhan: 0, other: 0, data: res[1].data.data }
        list.forEach(item => {
          if (item.amount >= 0) {
            if (item.dimension === '武汉') {
              params.wuhan += item.amount
            } else {
              params.other += item.amount
            }
          }
        })
        return params
      } else {
        Dialog.alert({
          message: data.errmsg
        }).then(() => {
          // on close
        })
        return false
      }
    },
    async two () {
      let result = await this.getTwoData()
      setTimeout(() => {
        this.case(result)
      }, 0)
      this.isActive1 = false
      this.isActive2 = true
      this.isActive3 = false
      this.isActive4 = false
      this.isActive5 = false
    },
    async getTwoData () {
      const { data } = await blfxtjList()
      if (data.errcode === 0) {
        const list = data.data
        let params = { data1: 0, data2: 0 }
        list.forEach(item => {
          if (item.AMOUNT >= 0) {
            console.log(item.AMOUNT)
            if (item.dimension === '发热门诊就诊数量') {
              params.data1 += item.AMOUNT
            } else {
              params.data2 += item.AMOUNT
            }
          }
        })
        return params
      } else {
        Dialog.alert({
          message: data.errmsg
        }).then(() => {
          // on close
        })
        return false
      }
    },
    async three () {
      let result = await this.getThreeData()
      setTimeout(() => {
        this.generation(result)
      }, 0)
      this.isActive1 = false
      this.isActive2 = false
      this.isActive3 = true
      this.isActive4 = false
      this.isActive5 = false
    },
    async getThreeData () {
      const { data } = await edblgcList()
      if (data.errcode === 0) {
        const list = data.data
        let params = { data1: 0, data2: 0 }
        list.forEach(item => {
          if (item.amount >= 0) {
            if (item.dimension === '本地二代病例') {
              params.data1 += item.amount
            } else {
              params.data2 += item.amount
            }
          }
        })
        return params
      } else {
        Dialog.alert({
          message: data.errmsg
        }).then(() => {
          // on close
        })
        return false
      }
    },
    async four () {
      const result = await this.getFourData()
      setTimeout(() => {
        this.age(result)
      }, 0)
      this.isActive1 = false
      this.isActive2 = false
      this.isActive3 = false
      this.isActive4 = true
      this.isActive5 = false
    },
    async getFourData () {
      const { data } = await nlfbList()
      if (data.errcode === 0) {
        const list = data.data
        let params = { data1: [], data2: [] }
        list.forEach(item => {
          let index = params.data1.indexOf(item.age_type)
          if (item.amount >= 0) {
            if (index < 0) {
              params.data1.push(item.age_type)
              let dataItem = params.data2[params.data1.length - 1]
              if (!dataItem) dataItem = 0
              dataItem += item.amount
              params.data2[params.data1.length - 1] = dataItem
            } else {
              params.data2[index] += item.amount
            }
          }
        })
        return params
      } else {
        Dialog.alert({
          message: data.errmsg
        }).then(() => {
          // on close
        })
        return false
      }
    },
    async five () {
      const result = await this.getFiveData()
      setTimeout(() => {
        this.morbidity(result)
      }, 0)
      this.isActive1 = false
      this.isActive2 = false
      this.isActive3 = false
      this.isActive4 = false
      this.isActive5 = true
    },
    async getFiveData () {
      const { data } = await nlxbList()
      if (data.errcode === 0) {
        const list = data.data
        let params = { data1: [], data2: [], data3: [] }
        list.forEach(item => {
          let index = params.data1.indexOf(item.age_type)
          console.log(item.age_type)
          if (index < 0) {
            params.data1.push(item.age_type)
            index = params.data1.length - 1
          }
          if (item.amount >= 0) {
            if (item.sex === '男') {
              let dataItem = params.data2[index]
              if (!dataItem) dataItem = 0
              dataItem += item.amount
              params.data2[index] = dataItem
            } else if (item.sex === '女') {
              let dataItem = params.data3[index]
              if (!dataItem) dataItem = 0
              dataItem += item.amount
              params.data3[index] = dataItem
            }
          }
        })
        return params
      } else {
        Dialog.alert({
          message: data.errmsg
        }).then(() => {
          // on close
        })
        return false
      }
    },
    circular1 (result) {
      let myChart = echarts.init(document.getElementById('circular1'))
      // 指定图表的配置项和数据

      let
        option = {
          tooltip: {
            trigger: 'item',
            formatter: '{b} :  {c} '
          },
          legend: {
            // orient: 'vertical',
            // top: 'middle',
            bottom: 10,
            left: 'center',
            data: [result.dimension]
          },
          grid: {
            left: '10',
            right: '10',
            containLabel: true
          },
          series: [
            {
              label: {
                position: 'inner',
                formatter: ' {b} \n {c}  '
              },
              type: 'pie',
              radius: '60%',
              center: ['50%', '40%'],
              selectedMode: 'single',
              data: [
                { value: result.amount, name: result.dimension, itemStyle: { color: '#005eff' } }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    circular2 (result) {
      let myChart = echarts.init(document.getElementById('circular2'))
      // 指定图表的配置项和数据

      let
        option = {
          tooltip: {
            trigger: 'item',
            formatter: '{b} : {c} '
          },
          legend: {
            // orient: 'vertical',
            // top: 'middle',
            bottom: 10,
            left: 'center',
            data: [result.dimension]
          },
          series: [
            {
              label: {
                position: 'inner',
                formatter: ' {b} \n {c}  '
              },
              type: 'pie',
              radius: '60%',
              center: ['50%', '40%'],
              selectedMode: 'single',
              data: [
                { value: result.amount, name: result.dimension, itemStyle: { color: '#ff7d2d' } }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    circular3 (result) {
      let myChart = echarts.init(document.getElementById('circular3'))
      // 指定图表的配置项和数据

      let
        option = {
          tooltip: {
            trigger: 'item',
            formatter: '{b} : {c} '
          },
          legend: {
            // orient: 'vertical',
            // top: 'middle',
            bottom: 10,
            left: 'center',
            data: [result.dimension]
          },
          series: [
            {
              label: {
                position: 'inner',
                formatter: ' {b} \n {c}  '
              },
              type: 'pie',
              radius: '60%',
              center: ['50%', '40%'],
              selectedMode: 'single',
              data: [
                { value: result.amount, name: result.dimension, itemStyle: { color: '#fff367' } }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    case (result) {
      let myChart = echarts.init(document.getElementById('case'))
      // 指定图表的配置项和数据

      let option = {
        title: {
          text: '病例发现途径',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          // orient: 'vertical',
          // top: 'middle',
          bottom: 10,
          left: 'center',
          data: ['发热门诊就诊数量', '主动筛查数量']
        },
        series: [
          {
            label: {
              position: 'inner'
            },
            type: 'pie',
            radius: '65%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            data: [
              { value: result.data1, name: '发热门诊就诊数量' },
              { value: result.data2, name: '主动筛查数量' }
            ]
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    generation (result) {
      let myChart = echarts.init(document.getElementById('generation'))
      // 指定图表的配置项和数据

      let option = {
        title: {
          text: '二代病例构成',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          // orient: 'vertical',
          // top: 'middle',
          bottom: 10,
          left: 'center',
          data: ['本地二代病例', '外地二代病例']
        },
        series: [
          {
            label: {
              position: 'inner'
            },
            type: 'pie',
            radius: '65%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            data: [
              { value: result.data1, name: '本地二代病例' },
              { value: result.data2, name: '外地二代病例' }
            ]
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    age (result) {
      let myChart = echarts.init(document.getElementById('age'))
      // 指定图表的配置项和数据

      let option = {
        title: {
          text: '年龄分布',
          left: 'center'
        },
        color: ['#3398DB'],
        tooltip: { trigger: 'axis' },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: result.data1,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '人数',
            type: 'bar',
            barWidth: '60%',
            data: result.data2
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    morbidity (result) {
      let myChart = echarts.init(document.getElementById('morbidity'))
      // 指定图表的配置项和数据

      let option = {
        title: {
          left: 'center',
          text: '发病数情况'
        },
        tooltip: { trigger: 'axis' },
        legend: {
          bottom: 10,
          left: 'center',
          data: ['男', '女']
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: result.data1
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '男',
            type: 'bar',
            data: result.data2
          },
          {
            name: '女',
            type: 'bar',
            data: result.data3
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    },
    wuhan (result) {
      let myChart = echarts.init(document.getElementById('wuhan'))
      // 指定图表的配置项和数据

      let option = {
        title: {
          text: '病例来源构成',
          left: 'center'
        },
        tooltip: { trigger: 'axis' },
        legend: {
          bottom: 10,
          left: 'center',
          data: ['武汉', '其他地区']
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: ['']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '武汉',
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              color: ['#090909']
            },
            data: [result.wuhan]
          },
          {
            name: '其他地区',
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              color: ['#090909']
            },
            data: [result.other]
          }
        ]
      }

      // 使用刚指定的配置项和数据显示图表
      myChart.setOption(option)
    }
  },
  mounted () {
    this.one()
  }
}
</script>

<style scoped>
    .header{
        text-align: center;
        line-height: 40px;
        height: 40px;
        font-size: 10px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
    .content{
        /*background-color: #efefef;*/
        padding-top: 10px;
    }
    .chart-item-1{
        width: 100%;
        height: 240px;
    }
    .chart-item-2{
        width: 100%;
        height: 120px;
    }
    .chart-item-3{
        width: 100%;
        height: 200px;
    }
    .title-item{
        padding: 10px;
        font-size: 14px;
        color: #333333;
    }
    .title-item span{
        border-left: 5px solid #8b0000;
        padding-left: 5px;
    }
    .recovery{
        height: 200px;
        border: black 1px solid;
    }
    .circular1{
        height: 60px;
        width: 60px;
        border: black 1px solid;
        border-radius: 50%;
    }
    .circular2{
        height: 60px;
        width: 60px;
        border: black 1px solid;
        border-radius: 50%;
    }
    .circular3{
        height: 60px;
        width: 60px;
        border: black 1px solid;
        border-radius: 50%;
    }
    .circular4{
        height: 60px;
        width: 60px;
        border: black 1px solid;
        border-radius: 50%;
    }
    .chart-div{
        padding: 0 10px;
    }
    .chart-div .chart-item-2,.chart-div .chart-item-1,.chart-div .chart-item,.chart-div .table-item{
        background-color: #ffffff;
    }
    .divider{
        /*width: 100%;*/
        /*height: 5px;*/
        /*background-color: rgb(204, 204, 204);*/
        /*margin-top: 10px;*/
    }
    .divider1{
        width: 100%;
        height: 5px;
        background-color: rgb(204, 204, 204);
    }
    .chart-div .van-col--12{
        padding: 0;
    }
    .van-col--5{
        width: 20%;
    }
    .van-col.van-col--5{
        background-color: #73a4f4;
        padding: 0 5px;
        margin-bottom: 10px;
    }
    .van-col.van-col--5.active{
        background-color: rgb(153, 204, 255);
    }
    .content{
        height: calc(100% - 10px);
    }
    .bg-white {
        height: calc(100% - 90px);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
    .bg-white  .chart-div{
        width: 100%;
        height: 200px;
    }
    .bg-white  .chart-div1{
        width: 100%;
        height: 300px;
    }
    .bg-white  .chart-div2{
        width: 100%;
        height: 100px;
    }
</style>
